home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / CAMPF.C < prev    next >
C/C++ Source or Header  |  1992-03-09  |  15KB  |  443 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : CAMPF.C
  4.  *
  5.  *      This is the actual minimization algorithm. Variation from the actual
  6.  *    CAMP algorithm is as follows:
  7.  *        1.    Minterms with adjacency 0 or 1 is minimised first
  8.  *        2.    Select next minterm with lowest adjacency wrt b-array
  9.  *        3.  If more than 1 such minterms, select one with the lowest
  10.  *            adjacency wrt to a-array
  11.  *        4.    To shrink the CPT, select an adjacent term, mi that has the
  12.  *            largest wi AND is already covered
  13.  *        5.  If more than one such adjacent terms are covered, select one
  14.  *        with the lowest adjacency wrt to b-array
  15.  *        6.  If non of the adjacent term is covered, select one with the
  16.  *        lowest adjacency wrt b-array from the uncovered adj term
  17.  *        at step 4.
  18.  *
  19.  * --------------------------------------------------------------------------
  20.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  21.  *    University.
  22.  *
  23.  *    You are free to use, copy and distribute this software and its
  24.  *    documentation providing that:
  25.  *
  26.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  27.  *
  28.  *        IT IS NOT MODIFIED IN ANY WAY.
  29.  *
  30.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  31.  *
  32.  *    This program is provided "AS IS" without any warranty, expressed or
  33.  *    implied, including but not limited to fitness for any particular
  34.  *    purpose.
  35.  *
  36.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  37.  *    appreciated. Please send to:
  38.  *
  39.  *        Boon-Tiong Tan or Othman Bin Ahmad
  40.  *        School of EEE
  41.  *        Nanyang Technological University
  42.  *        Nanyang Avenue
  43.  *        Singapore 2263
  44.  *        Republic of Singapore
  45.  *
  46.  ***************************************************************************/
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #define mask8 255
  52.  
  53. unsigned char   *campf(a, b)              /* pointer to a & b array passed */
  54. unsigned char   *a, *b;
  55.  
  56. {
  57.    unsigned short   pterm, ma, mb, *pm, *pm1, *point, pos, pos1;
  58.    unsigned  long   m, k, topow();
  59.    register  long   i, wo, wi;
  60.    register  char   j;
  61.          char   test;
  62.    unsigned  char   *c, *d, *e, *s, *temp, count, adj0, adji;
  63.    unsigned  char   n, adj, adjacency(), nspm, cover;
  64.    unsigned  char   *adjacent(), *reduce(), *cpt(), *ssm(), adj_of_mi();
  65.  
  66.  
  67.    mb = *(b+2)<<8 | *(b+1);            /* no. of minterms in b-array */
  68.  
  69.    n    = *a;                          /* no. of variables */
  70.    nspm = *(a+3);                      /* no. of bytes/minterm */
  71.    ma = *(a+2)<<8 | *(a+1);            /* no. of minterms in a-array */
  72.  
  73.    temp = (unsigned char *) malloc(nspm+1);        /* temporary storage */
  74.    if (temp == 0)
  75.       {
  76.      printf("Out of memory -- CAMPF, *temp\n");
  77.      printf("Program terminated - 1\n");
  78.      exit(0);
  79.       }
  80.  
  81.    s = (unsigned char *) malloc(4);                /* header of s-array */
  82.    if (s == 0)
  83.       {
  84.      printf("Out of memory -- CAMPF, *s\n");
  85.      printf("Program terminated - 2\n");
  86.      exit(0);
  87.       }
  88.  
  89.    pterm = 0;                                /* no. of product term */
  90.  
  91.    /***
  92.     ***  MINIMIZE ALL MINTERMS WITH ADJACENCY OF 0 & 1 FIRST
  93.     ***/
  94.  
  95.    for (i=0; i<mb; i++)                      /* for adj = 0 or 1 */
  96.       {
  97.      *b = n;                             /* assign back to n */
  98.  
  99.      memcpy(temp, (b+4+nspm*i), nspm);   /* pick a minterm from b-array */
  100.      c = adjacent(temp, a, 1);           /* obtain the adjacent terms */
  101.      adj = *(c+1);                       /* adjacency of minterm */
  102.  
  103.      if (adj <= 1)                       /* adjacency 0 or 1 */
  104.         {
  105.            d = cpt(c);                   /* generate CPT */
  106.  
  107.            s = (unsigned char *) realloc(s,4+n*(pterm+1));   /* more space */
  108.            if (s == 0)
  109.           {
  110.              printf("Out of memory -- CAMPF, *s\n");
  111.              printf("Program terminated - 3\n");
  112.              exit(0);
  113.           }
  114.            memcpy ((s+4+n*pterm),d,n);   /* add CPT to soln array */
  115.            pterm++;                      /* product term count */
  116.  
  117.            b = reduce(c,b,i);            /* remove minterms covered from b-array */
  118.            i = (char) *b;                /* index pointer of b-array */
  119.            mb = *(b+2)<<8 | *(b+1);      /* value of mb altered in b-array */
  120.  
  121.            free(d);                    /* free pointer */
  122.         }
  123.      free(c);                            /* free pointer */
  124.       }
  125.  
  126.     /***
  127.      ***  MINIMIZE MINTERMS WITH ADJACENCY GREATER THAN 1
  128.      ***/
  129.  
  130.     while (mb > 0)
  131.        {
  132.       adj0 = 255;                     /* max for 8-bit */
  133.  
  134.       *b = n;                         /* assign back to n */
  135.  
  136.       /***
  137.        ***  OBTAIN AN ARRAY, PM OF POSITION OF ADJACENT TERMS WITH THE LOWEST
  138.        ***  ADJACENCY WRT B-ARRAY
  139.        ***/
  140.  
  141.       for (i=0; i<mb; i++)
  142.          {
  143.         memcpy(temp, (b+4+nspm*i), nspm);    /* pick a minterm */
  144.         adji = adjacency(temp, b);           /* adj wrt b-array */
  145.  
  146.         if (adji<adj0)                       /* look for lowest adj */
  147.            {
  148.               if (adj0!=255)                 /* not the 1st term */
  149.              free(pm);
  150.  
  151.               adj0 = adji;                   /* update lowest adj */
  152.               count = 1;                        /* set count to 1 */
  153.               pm = (unsigned short *) malloc(2);   /* space for pos */
  154.               if (pm == 0)
  155.              {
  156.                 printf("Out of memory -- CAMPF, *pm\n");
  157.                 printf("Program terminated - 4\n");
  158.                 exit(0);
  159.              }
  160.               *pm = i;                       /* 1st pos of minterm */
  161.            }
  162.  
  163.         else if (adji==adj0)                 /* adjacency as low */
  164.            {
  165.               count++;                       /* no. of elements in pm-array */
  166.               pm = (unsigned short*) realloc(pm, 2*count);    /* more space */
  167.               if (pm == 0)
  168.              {
  169.                 printf("Out of memory -- CAMPF, *pm\n");
  170.                 printf("Program terminated - 5\n");
  171.                 exit(0);
  172.              }
  173.               *(pm+count-1) = i;             /* elements in pm-array */
  174.            }
  175.          }
  176.  
  177.       if (count==1)                  /* if only 1 element in pm-array */
  178.          pos = *pm;
  179.       else
  180.          {
  181.         /***
  182.          ***  FORM PM-ARRAY, SELECT THE 1ST ADJACENT TERM THAT HAS THE
  183.          ***  LOWEST ADJACENCY WRT A-ARRAY
  184.          ***/
  185.  
  186.         adj0 = 255;                      /* max for 8-bit */
  187.         for (i=0; i<count; i++)          /* do for all in pm-array */
  188.            {
  189.               pos1 = *(pm+i);                     /* pos of minterm */
  190.               memcpy(temp, (b+4+nspm*pos1), nspm);
  191.               adji = adjacency(temp, a);          /* adj wrt a-array */
  192.  
  193.               if (adji<adj0)                      /* new lowest found */
  194.              {
  195.                 adj0 = adji;                  /* update lowest */
  196.                 pos = *(pm+i);                /* position of required minterm */
  197.              }
  198.            }
  199.          }
  200.        free(pm);
  201.  
  202.        memcpy(temp, (b+4+nspm*pos), nspm);       /* pick the required minterm */
  203.  
  204.        /***
  205.         ***  GENERATE SSM AND TEST FOR COVERAGE BY FUNCTION
  206.         ***/
  207.  
  208.        c = adjacent(temp, a, 255);          /* obtain adjacent terms */
  209.        adj = *(c+1);                        /* adjacency of minterms */
  210.        d = cpt(c);                          /* generate CPT */
  211.        e = ssm(d);                          /* generate SSM */
  212.        m = topow(*(e+1));                   /* no. of SSM terms */
  213.  
  214.        for (j=0; j<m; j++)                  /* check for SSM coverage */
  215.           {
  216.          memcpy (temp, (e+4+nspm*j), nspm);
  217.          test = exist (temp, a, ma);
  218.          if (test == -1)                /* SSM term not in a-array */
  219.              break;
  220.           }
  221.  
  222.        /***
  223.         ***  ALL SSM COVERED BY THE FUNCTION, SELECT CPT AS PRODUCT TERM
  224.         ***/
  225.  
  226.        if (test == 0)                       /* all SSM's covered by fn */
  227.           {
  228.          if (m > 65536)                 /* too many SSM terms */
  229.             {
  230.                printf("Product Term Too Huge \n");
  231.                printf("Program terminated \n");
  232.                exit(0);
  233.             }
  234.          *(e+1) = (m-1) & mask8;        /* no. of SSM terms minus 1 */
  235.          *(e+2) = (m-1)>>8 & mask8;
  236.          b = reduce(e,b,i);             /* remove minterms covered from b-array */
  237.          mb = *(b+2)<<8 | *(b+1);       /* no. of minterms in b-array */
  238.  
  239.          s = (unsigned char *) realloc(s, 4+n*(pterm+1)); /* more space */
  240.          if (s == 0)
  241.             {
  242.                printf("Out of memory -- CAMPF, *s\n");
  243.                printf("Program terminated - 6\n");
  244.                exit(0);
  245.             }
  246.          memcpy((s+4+n*pterm),d,n);     /* add CPT to soln array */
  247.          pterm++;                       /* no. of product terms */
  248.  
  249.          free(d);                       /* free pointers */
  250.          free(e);
  251.           }
  252.        else
  253.           {
  254.          /***
  255.           ***  SSM NOT COVERED BY THE FUNCTION
  256.           ***/
  257.  
  258.          free(d);                       /* free pointer */
  259.          cover =0;                      /* coverage status */
  260.  
  261.          while (!cover)        /* do until shrinked SSM is covered */
  262.             {
  263.                /***
  264.             ***  OBTAIN AN ARRAY, PM OF POSITIONS OF ADJACENT TERMS, MI WITH THE LARGEST WI
  265.             ***/
  266.  
  267.                wo = -1;                        /* initial value */
  268.                for (j=0; j<adj; j++)           /* do for all adjacent terms */
  269.               {
  270.                  memcpy(temp,(c+4+nspm*(j+1)),nspm); /* pick adjacent term */
  271.  
  272.                  wi = adj_of_mi(temp,e,a);           /* obtain wi */
  273.                  if (wi>wo)
  274.                 {
  275.                    if (wo != -1)                 /* not the first */
  276.                       free(pm);
  277.                    wo = wi;                      /* new lowest value */
  278.                    count = 1;                    /* reset count to 1 */
  279.  
  280.                    pm = (unsigned short *) malloc(2);  /* space for pm */
  281.                    if (pm==0)
  282.                       {
  283.                      printf("Out of memory -- CAMPF, *pm\n");
  284.                      printf("Program terminated - 7\n");
  285.                      exit(0);
  286.                       }
  287.                    *pm = j;                      /* first element */
  288.                 }
  289.                  else if (wi==wo)                    /* wi as large */
  290.                 {
  291.                    count++;                      /* no. of element in pm-array */
  292.  
  293.                    pm = (unsigned short *) realloc (pm, 2*count);  /* more space */
  294.                    if (pm==0)
  295.                       {
  296.                      printf("Out of memory -- CAMPF, *pm\n");
  297.                      printf("Program terminated - 8\n");
  298.                      exit(0);
  299.                       }
  300.                    *(pm+count-1) = j;            /* elements of pm-array */
  301.                 }
  302.               }
  303.                free(e);                                  /* free pointer */
  304.  
  305.                /***
  306.             ***  DETERMINE FROM PM-ARRAY, AN ARRAY, PM1 OF POSITION OF ADJACENT TERMS
  307.             ***  THAT HAVE ALREADY BEEN COVERED
  308.             ***/
  309.  
  310.                pm1 = (unsigned short *) malloc(2);       /* space for pm1 */
  311.                if (pm1==0)
  312.               {
  313.                  printf("Out of memory -- CAMPF, *pm1\n");
  314.                  printf("Program terminated - 9\n");
  315.                  exit(0);
  316.               }
  317.  
  318.                k = 0;
  319.                for (j=0; j<count; j++)              /* do for all element in pm-array */
  320.               {
  321.                  memcpy(temp, (c+4+nspm*(1+ *(pm+j))), nspm);    /* pick adj term */
  322.                  test = exist(temp,b,mb);
  323.                  if (test == -1)                               /* already covered */
  324.                 {
  325.                    memcpy((pm1+k++), (pm+j), 2);       /* transfer pos to pm1 */
  326.  
  327.                    pm1 = (unsigned short *) realloc(pm1, 2*(k+1)); /*  more space */
  328.                    if (pm1==0)
  329.                       {
  330.                      printf("Out of memory -- CAMPF, *pm1\n");
  331.                      printf("Program terminated - 10\n");
  332.                      exit(0);
  333.                       }
  334.                 }
  335.               }
  336.  
  337.                /***
  338.             ***  SELECT FROM PM1-ARRAY (IF EMPTY, SELECT FROM PM-ARRAY),
  339.             ***  THE 1ST ADJACENT TERM, MI WITH THE LOWEST ADJACENCY WRT B-ARRAY
  340.             ***/
  341.  
  342.                if (k == 0)                     /* non covered, pm1 empty */
  343.               {
  344.                  k = count;                /* look for lowest adj term in pm-array */
  345.                  point = pm;
  346.               }
  347.                else
  348.               point = pm1;                 /* look for lowest adj term in pm1-array */
  349.  
  350.                adj0 = 255;                     /* max of 8-bit */
  351.                for (j=0; j<k; j++)             /* do for all in pm1/pm-array */
  352.               {
  353.                  memcpy(temp,(c+4+nspm*(1+*(point+j))),nspm);  /* pick an adj term */
  354.                  adji = adjacency(temp,b);                     /* obtain adjacency */
  355.  
  356.                  if (adji<=adj0)                        /* new lowest found */
  357.                 {
  358.                    adj0 = adji;                    /* update lowest */
  359.                    pos = *(point+j);               /* position of lowest adj term */
  360.                 }
  361.               }
  362.  
  363.                adj--;                            /* no. of adj terms in c-array */
  364.                *(c+1) = adj;                     /* adjacency after shrinking CPT */
  365.  
  366.                free(pm);                         /* free pointer */
  367.                free(pm1);
  368.  
  369.                /***
  370.             ***  SHRINK CPT (REMOVE 1 ADJACENT TERM FROM C-ARRAY), GENERATE NEW CPT, SSM
  371.             ***  AND TEST FOR COVERAGE BY FUNCTION
  372.             ***/
  373.  
  374.                memcpy((c+4+nspm*(1+pos)), (c+4+nspm*(2+pos)), nspm*(adj-pos));  /* shrink CPT */
  375.  
  376.                c = (unsigned char*) realloc(c, 4+nspm*(1+adj));   /* reduce space */
  377.                if (c==0)
  378.               {
  379.                  printf("Out of memory -- CAMPF, *c\n");
  380.                  printf("Program terminated - 11\n");
  381.                  exit(0);
  382.               }
  383.  
  384.                d = cpt(c);                  /* generate CPT */
  385.                e = ssm(d);                  /* generate SSM */
  386.                m = topow(*(e+1));           /* no. of SSM terms */
  387.  
  388.                for (i=0; i<m; i++)          /* check SSM coverage by function */
  389.               {
  390.                  memcpy(temp, (e+4+nspm*i), nspm);  /* pick SSM term */
  391.                  test = exist(temp,a,ma);
  392.                  if (test == -1)                    /* SSM term not covered */
  393.                 break;
  394.               }
  395.  
  396.                /***
  397.             ***  SHRINKED SSM COVERED BY FUNCTION, REDUCE B-ARRAY
  398.             ***  AND SELECT SHRINKED CPT AS PRODUCT TERM
  399.             ***/
  400.  
  401.                if (test==0)                   /* SSM covered */
  402.               {
  403.                  if (m > 65536)                  /* too many SSM terms */
  404.                 {
  405.                    printf("Product Term Too Huge \n");
  406.                    printf("Program terminated \n");
  407.                    exit(0);
  408.                 }
  409.                  *(e+1) = (m-1) & mask8;         /* no. of SSM terms minus 1 */
  410.                  *(e+2) = (m-1)>>8 & mask8;
  411.                  b = reduce(e, b, i);     /* remove minterms covered from b-array */
  412.                  mb = *(b+2)<<8 | *(b+1); /* no. of minterms in b-array */
  413.  
  414.                  s = (unsigned char *) realloc(s, 4+n*(pterm+1));  /* more space */
  415.                  if (s==0)
  416.                 {
  417.                    printf("Out of memory -- CAMPF, *s\n");
  418.                    printf("Program terminated - 12\n");
  419.                    exit(0);
  420.                 }
  421.                  memcpy ((s+4+n*pterm), d, n);      /* add shrinked CPT to soln array */
  422.                  pterm++;                           /* no. of product terms */
  423.  
  424.                  cover = 1;                         /* coverage status */
  425.                  free(e);                           /* free pointer */
  426.               }
  427.                free (d);                      /* free pointer */
  428.             }
  429.           }
  430.       free(c);                   /* free pointer */
  431.       }
  432.    *s = n;                           /* no. of variables */
  433.    *(s+1) = pterm & mask8;           /* no. of product terms in 2 bytes */
  434.    *(s+2) = pterm>>8 & mask8;
  435.  
  436.    free(temp);                       /* free pointer */
  437.    free(a);
  438.    free(b);
  439.  
  440.    return(s);                        /* return solution array */
  441. }
  442.  
  443.